<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="监测站ID" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入监测站ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['hma:temperature:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hma:temperature:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hma:temperature:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['hma:temperature:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="router.push({ path: '/hma/tempchart' })"
        >历史趋势图</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="temperatureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="readingId" />
      <el-table-column label="监测站ID" align="center" prop="stationId" />
      <el-table-column label="表层水温测量深度 (米)" align="center" prop="surfaceDepth" />
      <el-table-column label="表层水温 (摄氏度)" align="center" prop="surfaceTemperature" />
      <el-table-column label="底层水温测量深度 (米)" align="center" prop="bottomDepth" />
      <el-table-column label="底层水温 (摄氏度)" align="center" prop="bottomTemperature" />
      <el-table-column label="不同深度的水温测量间隔" align="center" prop="depthInterval" />
      <!-- <el-table-column label="存储不同深度的温度值" align="center" prop="temperatureProfile" /> -->
      <el-table-column label="使用的温度计类型" align="center" prop="thermometerType">
        <template #default="scope">
          <dict-tag :options="water_temp_type" :value="scope.row.thermometerType"/>
        </template>
      </el-table-column>
      <el-table-column label="冬季结冰状况描述" align="center" prop="iceCondition" />
      <el-table-column label="极端温度事件记录" align="center" prop="extremeTempEvents" />
      <!-- <el-table-column label="温度梯度 (摄氏度/米)" align="center" prop="tempGradient" /> -->
      <!-- <el-table-column label="水体热量总量 (焦耳)" align="center" prop="heatContent" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hma:temperature:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hma:temperature:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水温监测对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="temperatureRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="station_id" prop="stationId">
          <el-input v-model="form.stationId" placeholder="请输入关联到 hma_stations 表中的 station_id" />
        </el-form-item>
        <el-form-item label="测量深度 (米)" prop="surfaceDepth">
          <el-input v-model="form.surfaceDepth" placeholder="请输入表层水温测量深度 (米)" />
        </el-form-item>
        <el-form-item label="表层水温" prop="surfaceTemperature">
          <el-input v-model="form.surfaceTemperature" placeholder="请输入表层水温 (摄氏度)" />
        </el-form-item>
        <el-form-item label="底层水温测量深度" prop="bottomDepth">
          <el-input v-model="form.bottomDepth" placeholder="请输入底层水温测量深度 (米)" />
        </el-form-item>
        <el-form-item label="底层水温" prop="bottomTemperature">
          <el-input v-model="form.bottomTemperature" placeholder="请输入底层水温 (摄氏度)" />
        </el-form-item>
        <el-form-item label="不测量间隔" prop="depthInterval">
          <el-input v-model="form.depthInterval" placeholder="请输入不同深度的水温测量间隔" />
        </el-form-item>
        <el-form-item label="的温度值" prop="temperatureProfile">
          <file-upload v-model="form.temperatureProfile"/>
        </el-form-item>
        <el-form-item label="温度计类型" prop="thermometerType">
          <el-select v-model="form.thermometerType" placeholder="请选择使用的温度计类型">
            <el-option
              v-for="dict in water_temp_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="冬季结冰状况描述" prop="iceCondition">
          <el-input v-model="form.iceCondition" placeholder="请输入冬季结冰状况描述" />
        </el-form-item>
        <el-form-item label="温度梯度 (摄氏度/米)" prop="tempGradient">
          <el-input v-model="form.tempGradient" placeholder="请输入温度梯度 (摄氏度/米)" />
        </el-form-item>
        <el-form-item label="整个水体所含有的热量总量 (焦耳)">
          <editor v-model="form.heatContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Temperature">
import { listTemperature, getTemperature, delTemperature, addTemperature, updateTemperature } from "@/api/hma/temperature";
import { useRouter } from 'vue-router';
const router = useRouter();

const { proxy } = getCurrentInstance();
const { water_temp_type } = proxy.useDict('water_temp_type');

const temperatureList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stationId: null,
    createTime: null,
  },
  rules: {
    stationId: [
      { required: true, message: "关联到 hma_stations 表中的 station_id不能为空", trigger: "blur" }
    ],
    surfaceDepth: [
      { required: true, message: "表层水温测量深度 (米)不能为空", trigger: "blur" }
    ],
    surfaceTemperature: [
      { required: true, message: "表层水温 (摄氏度)不能为空", trigger: "blur" }
    ],
    bottomDepth: [
      { required: true, message: "底层水温测量深度 (米)不能为空", trigger: "blur" }
    ],
    bottomTemperature: [
      { required: true, message: "底层水温 (摄氏度)不能为空", trigger: "blur" }
    ],
    depthInterval: [
      { required: true, message: "不同深度的水温测量间隔不能为空", trigger: "blur" }
    ],
    temperatureProfile: [
      { required: true, message: "存储不同深度的温度值不能为空", trigger: "blur" }
    ],
    thermometerType: [
      { required: true, message: "使用的温度计类型不能为空", trigger: "change" }
    ],
    iceCondition: [
      { required: true, message: "冬季结冰状况描述不能为空", trigger: "blur" }
    ],
    extremeTempEvents: [
      { required: true, message: "极端温度事件记录不能为空", trigger: "blur" }
    ],
    tempGradient: [
      { required: true, message: "温度梯度 (摄氏度/米)不能为空", trigger: "blur" }
    ],
    heatContent: [
      { required: true, message: "整个水体所含有的热量总量 (焦耳)不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询水温监测列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listTemperature(queryParams.value).then(response => {
    temperatureList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    readingId: null,
    stationId: null,
    surfaceDepth: null,
    surfaceTemperature: null,
    bottomDepth: null,
    bottomTemperature: null,
    depthInterval: null,
    temperatureProfile: null,
    thermometerType: null,
    iceCondition: null,
    extremeTempEvents: null,
    tempGradient: null,
    heatContent: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("temperatureRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.readingId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加水温监测";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _readingId = row.readingId || ids.value
  getTemperature(_readingId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改水温监测";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["temperatureRef"].validate(valid => {
    if (valid) {
      if (form.value.readingId != null) {
        updateTemperature(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTemperature(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _readingIds = row.readingId || ids.value;
  proxy.$modal.confirm('是否确认删除水温监测编号为"' + _readingIds + '"的数据项？').then(function() {
    return delTemperature(_readingIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hma/temperature/export', {
    ...queryParams.value
  }, `temperature_${new Date().getTime()}.xlsx`)
}

getList();
</script>
